package org.lucene.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckServlet extends HttpServlet {
    public String[] usernameList;
	
    
    //doGet方法处理用户的请求
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//获取用户名
		String username = (String)request.getParameter("username");
		//获取用户密码
		String password = (String)request.getParameter("password");
		//获取用户确认密码
		String repassword = (String)request.getParameter("repassword");
		//获取用户email
		String email = (String)request.getParameter("email");
		
		//若用户名为空 提示用户
		if(username.equals("")||username==null){
			request.setAttribute("error.message", "用户名不允许为空！");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
			
		}
		else if(password.equals("")||password==null){
			request.setAttribute("error.message", "两次输入密码不一致!");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
		}//如两次密码不一致提示用户
		else if(!password.equals(repassword)){
			request.setAttribute("error.message", "两次输入密码不一致");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
			
		}else if (this.IsContain(username)){
			request.setAttribute("error.message", "您输入的用户名已经存在");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
			
		}else{
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("success.jsp");
			requestDispatcher.forward(request, response);
		}
	}

	
	private boolean IsContain(String param) {
		for(int i = 0;i<usernameList.length;i++){
			if(usernameList[i].equals(param)){
				return true;
			}
			else
			continue;
		}	
		return false;
	}
    public void init(ServletConfig config) throws ServletException{
    	usernameList = new String[]{"zhangsan","lisi","wangwu","zhaoliu"};
    }

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
